package cn.ecut.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;



@WebServlet("/requestMethod03")
public class RequestMethodTest03 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Enumeration<String> parameterNames = request.getParameterNames();
        while( parameterNames.hasMoreElements() ){
            //  获取到参数名称
            String parameterName = parameterNames.nextElement();
            // 根据参数名，获取参数值( 仅仅获取一个 )
            String parameter = request.getParameter(parameterName);
            System.out.println( parameterName + " : " + parameter );
        }
        /**
         * password : 123456
         * hometown : jiangxi
         * gender : male
         * hobbies : eat
         * username : 张三
         */
        // 当参数中存在多个值的时候，那么就可以通过 getParameterValues 获取到 对应的值 组成的数组
        String[] hobbies = request.getParameterValues("hobbies");
        System.out.println(Arrays.toString( hobbies )); //[eat, sleep]

        // 可以将 这个 参数封装到Map集合中，从而进行查看
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 迭代Map
        for ( Map.Entry<String, String[]> entry : parameterMap.entrySet() ) {
            System.out.println( entry.getKey() + " : " + Arrays.toString( entry.getValue() ));
        }
        /**
         * password : [123456]
         * hometown : [jiangxi]
         * gender : [male]
         * hobbies : [eat, sleep]
         * username : [张三]
         */
        // 更加常见的做法 ： request.getParameter( name ) ;

        String characterEncoding = request.getCharacterEncoding();

        request.setCharacterEncoding( "UTF-8");
    }
}
